<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/doge.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:onExit()"> 退出登陆</a>        <!-- a标签不一定全部被识别，onclick不一定起效，但用js标签可以 -->
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="wengzhang"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right" id="artlistDiv">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
        </div>
    </div>
</body>
<script>
    var maxlength=80;
    function mySubstr(content){
        if(content.length>=maxlength){
            return content.substr(0,maxlength);
        }
        return content;
     }
    function initList(){
        jQuery.ajax({
            url:"/art/mylist",
            type:"POST",
            data:{},
            success:function(result){
                console.dir(result);
                if(result.code==200&&result.data!=null&&result.data.length>0){
                    var html="";
                    result.data.forEach(function(item){
                        html+='<div class="blog">'+
                                '<div class="title">'+item.title+'</div>'+
                                '<div class="date">'+item.createtime+'</div>'+'<div class="desc">'+
                                    mySubstr(item.content)+' </div>'+
                                    '<div style="text-align: center;margin-top: 50px;">'+
                                    '<a href="blog_content.html?id='+item.id+'">查看详情</a>&nbsp;&nbsp;'+
                                    '<a href="blog_update.html?id='+item.id+
                                    '">修改</a>&nbsp;&nbsp;<a href="javascript:myDel('+item.id
                                    +')">删除</a></div>'
                                    +'</div>';
                        }); 
                   jQuery("#artlistDiv").html(html);
                }else{
                    jQuery("#artlistDiv").html("<h1>暂无数据/<h1>");// 往这个标签里添加暂无数据的标签
                }
            },
            error:function(err){
                    if(err!=null&&err.status==401){
                       // alert(JSON.stringify(err));
                       alert("用户未登陆");
                       location.href="/login.html";
                }      
            }
        });
    }
     initList();
     
        
    function getT(uid){
        var total=1;
        jQuery.ajax({
            url:"/art/gett",
            type:"POST",
            data:{
                "uid":uid
            },
            success:function(result){
                if(result.code==200&&result.data!=null){
                    total=result.data;
                    jQuery("#wengzhang").text(total);
                }
            }
        });
    }

     function myInfo(){
        jQuery.ajax({
                url:"/user/myinfo",
                type:"POST",
                data:{},
                success:function(result){
                    if(result.code==200&&result.data!=null){
                        jQuery("#username").text(result.data.username);
                        getT(result.data.id);
                    }
                }
            });
     }

     myInfo();

     function onExit(){
        if(confirm("确认退出?")){
            // alert("退出成功");
            jQuery.ajax({
                url:"/user/logout",
                type:"POST",
                data:{},
                success:function(result){
                    location.href="/login.html";
                },
                error:function(err){
                    if(err!=null&&err.status==401){
                       // alert(JSON.stringify(err));
                       alert("用户未登陆");
                       location.href="/login.html";
                    }      
                }
            });
        }
     }
     function myDel(id){
        if(confirm("确认删除?")){
            // alert("退出成功");
            jQuery.ajax({
                url:"/art/del",
                type:"POST",
                data:{"id":id},
                success:function(result){
                    alert("删除成功");
                    location.href="/myblog_list.html";
                },
                error:function(err){
                    if(err!=null&&err.status==401){
                       // alert(JSON.stringify(err));
                       alert("用户未登陆");
                       location.href="/login.html";
                    }      
                }
            });
        }
     }
</script>
</html>